package net.suncaper.demo.controller;

import net.suncaper.demo.domain.Hotel;
import net.suncaper.demo.domain.RoomType;
import net.suncaper.demo.service.HotelService;
import net.suncaper.demo.service.RoomTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/hotel")
public class HotelController {
    @Autowired
    private HotelService hotelService;

    @Autowired
    private RoomTypeService roomTypeServicelService;

    @GetMapping("/search")
    public  String userPage(HttpServletRequest request,Model model){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date1=format.format(new Date());
        Calendar c1=Calendar.getInstance();
        c1.setTime(new Date());
        c1.add(Calendar.DAY_OF_MONTH,1);
        String date2=format.format(c1.getTime());
        model.addAttribute("date1",date1);
        model.addAttribute("date2",date2);
        return "pages/hotel/search";
    }

    @PostMapping("/search")
    public String search(@RequestParam("city")String city, @RequestParam("key")String key,
                         @RequestParam("roomnum")int roomnum,Model model, HttpServletRequest request,
                         @RequestParam("time1")Date checkin,@RequestParam("time2" )Date checkout) {

        if(checkout==null){
            Calendar temp=Calendar.getInstance();
            temp.setTime(new Date());
            temp.set(Calendar.MINUTE,0);
            temp.set(Calendar.HOUR,0);
            temp.set(Calendar.SECOND,0);
            temp.add(Calendar.DAY_OF_MONTH,1);
            checkout=temp.getTime();
        }
        //System.out.println(checkin);
        //System.out.println(checkout);
        Long cityID=hotelService.selectByCityName(city);
        key="'%"+key+"%'";


        HttpSession session=request.getSession();
        session.setAttribute("cityID",cityID);
        session.setAttribute("cityName",city);
        session.setAttribute("key",key);
        session.setAttribute("checkin",checkin);
        session.setAttribute("checkout",checkout);
        session.setAttribute("roomnum",roomnum);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String timein = format.format(checkin);
        String timeout = format.format(checkout);
       // System.out.println(timein);
       // System.out.println(timeout);

        Calendar c1 = Calendar.getInstance();
        c1.setTime(checkin);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(checkout);
        ArrayList<Hotel>hotels= (ArrayList<Hotel>) hotelService.selectByInput1(cityID,key,"'"+timein+"'",roomnum);
        c1.add(Calendar.DAY_OF_MONTH, 1);

        while(c1.compareTo(c2)<0){
            ArrayList<Hotel>temp= (ArrayList<Hotel>) hotelService.selectByInput1(cityID,key,"'"+format.format(c1.getTime())+"'",roomnum);
            for(int i=0;i<hotels.size();i++){
                if(!temp.contains(hotels.get(i)))hotels.remove(i);
            }

            c1.add(Calendar.DAY_OF_MONTH, 1);
        }
      //  for(int i=0;i<hotels.size();i++) System.out.println(hotels.get(i).getHotelname());
        model.addAttribute("hotels",hotels);

//        JSONArray jsonArray = new JSONArray();
//        for(Hotel h : hotels){
//            //System.out.println(h.getHotelname());
//            JSONArray temp = new JSONArray();
//            JSONObject t=new JSONObject();
//            t.put("w",1);
//            t.put("h",21);
//            t.put("l",0);
//            t.put("t",0);
//            t.put("x",6);
//            t.put("lb",5);
//            temp.put(t);
//            JSONObject jo = new JSONObject();
//            jo.put("hotelID",h.getHotelid());
//            jo.put("title", h.getHotelname());
//            jo.put("content",h.getAddress());
//            jo.put("point",h.getLongtitude().toString()+'|'+h.getLongtitude().toString());
//            jo.put("isopen",1);
//            jo.put("icon",temp);
//            jsonArray.put(jo);
//
//        }
//        System.out.println(jsonArray.toString());
//        model.addAttribute("markerArr",jsonArray);


        return "pages/hotel/searchresult";
    }

    @PostMapping("/searchdetail")
    public String serachdetail(HttpServletRequest request,@RequestParam("pricelow") String lowprice
            ,@RequestParam("pricehigh") String highprice,@RequestParam("score") Double score,
                               @RequestParam("star") int star, Model model){
        HttpSession session= request.getSession();
        Long cityID= (Long) session.getAttribute("cityID");
        String key= session.getAttribute("key").toString();
        Date checkin= (Date) session.getAttribute("checkin");
        Date checkout= (Date) session.getAttribute("checkout");
        int roomnum= (int) session.getAttribute("roomnum");


        double pricelow,pricehigh;
        if(lowprice=="")pricelow=0;
        else {
            try
            {
                pricelow=Double.parseDouble(lowprice);
            }
            catch(NumberFormatException ex){
                pricelow=0;
            }
        }
        if(highprice=="")pricehigh=1000000000;
        else {
            try
            {
                pricehigh=Double.parseDouble(highprice);
            }
            catch(NumberFormatException ex){
                pricehigh=0;
            }
        }
        if(pricehigh<pricelow){
            double temp;
            temp=pricehigh;
            pricehigh=pricelow;
            pricelow=temp;
        }

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String timein = format.format(checkin);
        ArrayList<Hotel> hotels=(ArrayList<Hotel>) hotelService.selectByInput2(cityID,key,"'"+timein+"'",pricelow,pricehigh,score,star,roomnum);

        Calendar c1 = Calendar.getInstance();
        c1.setTime(checkin);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(checkout);
        c1.add(Calendar.DAY_OF_MONTH, 1);
        while(c1.compareTo(c2)<0){
            ArrayList<Hotel>temp= (ArrayList<Hotel>) hotelService.selectByInput1(cityID,key,"'"+format.format(c1.getTime())+"'",roomnum);
            for(int i=0;i<hotels.size();i++){
                if(!temp.contains(hotels.get(i)))hotels.remove(i);
            }

            c1.add(Calendar.DAY_OF_MONTH, 1);
        }
      //  for(int i=0;i<hotels.size();i++) System.out.println(hotels.get(i).getHotelname());
        model.addAttribute("hotels",hotels);
        //System.out.println(a);
        return "/pages/hotel/searchresult";
    }

    @GetMapping("/detail/{id}")
    public  String hotelshow(@PathVariable("id") String id, Model model,HttpServletRequest request){
        Long hotelID=Long.parseLong(id);
        HttpSession session=request.getSession();
        Long cityID;
        cityID= (Long) session.getAttribute("cityID");
        Hotel hotel=hotelService.selectByID(cityID,hotelID);
        ArrayList<RoomType>roomtypes= (ArrayList<RoomType>) roomTypeServicelService.selectByID(cityID,hotelID);
        model.addAttribute("hotel",hotel);
        model.addAttribute("roomtypes",roomtypes);

        return "/pages/hotel/detail.html";
    }


    @ResponseBody
    private List<Hotel> allHotel(){
        return hotelService.allHotel();
    }

    @RequestMapping
    public String hotelPage(Model model){
        List<Hotel> hotels=hotelService.allHotel();
        model.addAttribute("hotels",hotels);
        return "pages/hotel/list.html";
    }

}